#include "osgi-predefined.h"

namespace MicroKernelRuntime
{

typedef struct SERVICE_BASEINFO
{
        char* pAddr;
        int protocol;			//refer commands.h, VAL_TCP/VAL_UDP/VAL_HTTP/..
        int port;

}SERVICE_BASEINFO;
typedef struct SADDRESS
{
    char* ip;
    int port;
} SADDRESS;



typedef void (*OnServiceInstanceRegistered)(char* serviceName, char* serviceId);
typedef void (*OnServiceInstanceUnRegistered)(char* serviceName, char* serviceId);
typedef void (*OnServiceInstanceRoleChanged)(char* serviceName, char* serviceId, int role);

//#define IID_RemoteService_Mgr "IID_RemoteService_Mgr"
//#define SID_RemoteService_Mgr "SID_RemoteService_Mgr"

#define RoundRobin 1
#define SrcIP_strategy 2
#define Random_strategy 3
#define HotStandBy 4

//分布式均衡管理服务调用通知接口
OSGI_INTERFACE IServiceEventNotify
{
	//服务器实例注册
	virtual int OSGI_API OnServiceRegistered(
				char * pServiceName,
				char * pServiceInstanceName) = 0;

	//服务器实例注销
	virtual int OSGI_API OnServiceUnRegistered(
				char * pServiceName,
				char *pServiceInstanceName) = 0;
};

#define MASTER_ROLE 1
#define SLAVE_ROLE 2

//热备式服务调用通知接口
OSGI_INTERFACE IServiceRoleChangeNotify
{
        //服务实例注册
        virtual int OSGI_API OnServiceRoleChanged(
                                char * pServiceName,
                                char * pServiceInstanceName,
                                int role) = 0;
};


OSGI_INTERFACE ICluster : public IService
{
        //服务实例注册
        virtual int OSGI_API RegServiceInstance(
                                SERVICE_BASEINFO *pBaseInstanceInfo,
                                IDictionary * extInstanceInfo,
                                char ** ppServiceInstanceID) = 0;

        //服务实例注销
        virtual int OSGI_API UnRegServiceInstance(
                                char * pServiceInstanceID) = 0;

        //获取指定服务实例信息
        virtual int OSGI_API GetServiceInstance(
                                char * pServiceInstanceID,
                                SERVICE_BASEINFO * pBaseInstanceInfo,
                                IDictionary ** exInstanceInfo,
                                IDictionary ** ppServiceInstanceStatus) = 0;

        //获取集群某类型服务信息列表
        virtual int OSGI_API GetServiceInstances(
                                char *** pppServiceInstanceInfoSet,
                                int * pServiceInstanceCount) = 0;

        //注册服务通知
        virtual int OSGI_API RegisterServiceInstanceNotify(IServiceEventNotify* pNotify) = 0;

        //反注册服务通知
        virtual int OSGI_API UnRegisterServiceInstanceNotify(IServiceEventNotify* pNotify) = 0;

        //上报服务状态
        virtual int OSGI_API ReportServiceStatus(
                                char * pServiceInstanceID,
                                IDictionary * serviceInstanceStatus) = 0;

        //释放服务基本信息
        virtual int OSGI_API ReleaseServiceBasicInfo(
                                SERVICE_BASEINFO * pBaseInstanceInfo) = 0;

        //释放服务实例内存
        virtual int OSGI_API ReleaseServiceInstances(
                                char ** ppServiceInstanceInfoSet) = 0;

        virtual void OSGI_API Destroy() = 0;
};



#define IID_LoadBalanceCluster "IID_LoadBalanceCluster"
//分布式服务 均衡管理
OSGI_INTERFACE ILoadBalanceService : public IService
{
        //基于集群负载均衡策略获取服务实例（负载均衡策略已注册）
        virtual int OSGI_API GetServiceInstance(
                                SERVICE_BASEINFO * ppBaseInstanceInfo) = 0;

        virtual ICluster * OSGI_API GetCluster() = 0;

        virtual void OSGI_API Destroy() = 0;

};


#define IID_ActiveStandbyCluster "IID_ActiveStandbyCluster"
#define SingleActive 1
//自定义负载均衡策略
OSGI_INTERFACE IActiveStandbyService : public IService
{
    //获取服务角色
    virtual int OSGI_API GetServiceRole(
                char * pServiceInstanceName,
                int * role) = 0;

    //注册服务角色变化通知
    virtual int OSGI_API RegisterServiceRoleChangeNotify(
                char * pServiceInstanceName,
                IServiceRoleChangeNotify * pNotify) = 0;

    //注销服务角色变化通知
    virtual int OSGI_API UnRegisterServiceRoleChangeNotify(
                char * pServiceInstanceName,
                IServiceRoleChangeNotify * pNotify) = 0;

    virtual void OSGI_API Destroy() = 0;

};


#define IID_ClusterFactory "IID_ClusterFactory"
#define SID_ClusterFactory "SID_ClusterFactory"
#define LoadBalance 1
#define ActiveStandby 2

OSGI_INTERFACE IClusterFactory : public IService
{
    //工厂类，根据服务名和类型创建相应的服务管理器
    virtual ICluster * OSGI_API CreateCluster(
                const char * pServiceName) = 0;

    virtual ILoadBalanceService * OSGI_API CreateLoadBalanceService(
                const char * pServiceName,
                int strategy) = 0;

    virtual IActiveStandbyService * OSGI_API CreateActiveStandbyService(
                const char * pServiceName,
                int strategy) = 0;

    virtual void OSGI_API Destroy() = 0;
};

}












